package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 完成文件的上传
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadFile(MultipartFile file) throws IOException {
        WmUser wmUser = WmThreadLocalUtil.getUser();
        if(null == wmUser){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        String fileName = UUID.randomUUID().toString().replaceAll("-","");
        String sub = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        String url = fileStorageService.uploadImgFile("",
                fileName + sub, file.getInputStream());

        WmMaterial entity = new WmMaterial();
        entity.setCreatedTime(new Date());
        entity.setIsCollection((short)0);
        entity.setType((short)0);

        entity.setUserId(wmUser.getId());
        entity.setUrl(url);

        wmMaterialMapper.insert(entity);

        return ResponseResult.okResult(entity);
    }

    /**
     * 根据条件完成分页查询
     *
     * @param dto
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1. 检查分页的参数是否合法
        dto.checkParam();

        //2. 构建分页
        IPage page = new Page(dto.getPage(), dto.getSize());

        //3. 构建查询的条件
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(dto.getIsCollection() != null){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        WmUser wmUser = WmThreadLocalUtil.getUser();
        if(null == wmUser){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        lambdaQueryWrapper.eq(WmMaterial::getUserId, wmUser.getId());
        //4. 查询

        wmMaterialMapper.selectPage(page, lambdaQueryWrapper);
        //5. 封装返回的结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());

        responseResult.setData(page.getRecords());

        return responseResult;
    }
}
